package com.gimi.cloud.bbp.client.service.mall.impl;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.client.service.mall.MallMessageService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.MyBeanUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallMessageDao;
import com.gimi.cloud.bbp.dto.mall.MallMessageDTO;
import com.gimi.cloud.bbp.entity.mall.MallMessage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class MallMessageServiceImpl implements MallMessageService{
	
	@Autowired private MallMessageDao dao;
	
	@Override
	@Transactional
	public PageInfo<MallMessageDTO> getMessageListByReceiveUser(MallMessageDTO mallMessageDTO) {
		PageHelper.startPage(mallMessageDTO.getPage(), mallMessageDTO.getPageSize());
		return new PageInfo<>(dao.getMessageListByReceiveUser(mallMessageDTO.get_userId()));
	}
	
	@Override
	@Transactional
	public void userDelMessageByMeId(MallMessageDTO mallMessageDTO) {
		MallMessage mallMessage = dao.getMessageByMeId(mallMessageDTO.getMeId());
		if (mallMessage == null) {
			throw new MsgException(ResponseCodeEnum.NO_DATA);
		}
		mallMessage.setMeDelType(1);
		dao.baseUpdateByPrimaryKey(mallMessage);
	}
	
	@Override
	@Transactional
	public MallMessageDTO getMessageByMeId(MallMessageDTO mallMessageDTO) {
		MallMessage mallMessage = dao.getMessageByMeId(mallMessageDTO.getMeId());
		if (mallMessage == null) {
			throw new MsgException(ResponseCodeEnum.NO_DATA);
		}
		mallMessage.setMeStatus(1);//标记为已读
		dao.baseUpdateByPrimaryKey(mallMessage);
		MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(mallMessage, mallMessageDTO);
		return mallMessageDTO;
	}
	
	@Override
	@Transactional
	public Long getMessageByUserIdAndStatus(MallMessageDTO mallMessageDTO) {
		return dao.getMessageByUserIdAndStatus(mallMessageDTO.get_userId());
	}

}
